/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.monitor

import cool.taomu.command.JvmInfoMqttCallback
import cool.taomu.config.entity.ConfigEntity
import cool.taomu.entity.ServerInfo
import cool.taomu.mqtt.mqttv3.MqttV3Service
import cool.taomu.utils.RuntimeInfo
import java.util.Timer
import java.util.concurrent.ExecutorService
import java.util.concurrent.TimeUnit
import org.apache.commons.lang3.SerializationUtils

class MonitorJvm {

	def getJvmInfo() {
		return new RuntimeInfo();
	}

	def start(long duration,ExecutorService es,ConfigEntity entity) {
		var timer = new Timer();
		var d = TimeUnit.SECONDS.toSeconds(duration) * 1000;
		val mqtt = entity.mqtt;
		MqttV3Service.subscriber(es,mqtt.host,mqtt.port,mqtt.username,mqtt.password,JvmInfoMqttCallback);
		timer.schedule([
			var info = new ServerInfo();
			info.host = entity.rpc.host;
			info.port = entity.rpc.port;
			info.runtimeInfo = getJvmInfo();
			var  bytes = SerializationUtils.serialize(info);
			MqttV3Service.sender("taomu/jvm/info",mqtt.host,mqtt.port,mqtt.username,mqtt.password,bytes);
		], 0, d);
	}
}
